package com.sg.business.model;

import org.bson.types.ObjectId;

import com.mobnut.db.model.IContext;
import com.mobnut.db.model.ModelService;
import com.mobnut.db.model.PrimaryObject;

public class DummyDeliverableDef extends PrimaryObject {
	public static final String F_DOCUMENT_NUMBER = "documentnumber";
	public static final String F_FOLDER_ID = "folder_id";
	public static final String F__SUMMARY = "_summary";
	public static final String F_WORKDELIVERABLEDEFINITION_ID = "deliverabled_id";

	@Override
	public void doInsert(IContext context) throws Exception {
	}

	@Override
	public void doRemove(IContext context) throws Exception {
	}

	@Override
	public boolean doSave(IContext context) throws Exception {
		return true;
	}

	@Override
	public void doUpdate(IContext context) throws Exception {
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getAdapter(Class<T> adapter) {
		if (adapter.equals(Document.class)) {
			Document document = makeDocument(null);
			return (T) document;
		} else if (adapter.equals(Deliverable.class)) {
			Deliverable deliverable = makeDeliverable(null);

			return (T) deliverable;
		}
		return super.getAdapter(adapter);
	}

	private Deliverable makeDeliverable(Deliverable po) {
		ObjectId _id = getObjectIdValue(F_WORKDELIVERABLEDEFINITION_ID);
		if (_id != null) {
			WorkDeliverableDefinition deliverableDef = ModelService
					.createModelObject(WorkDeliverableDefinition.class, _id);

			po = deliverableDef.makeDeliverable(po);
		}

		return po;
	}

	private Document makeDocument(Document po) {
		ObjectId _id = getObjectIdValue(F_WORKDELIVERABLEDEFINITION_ID);
		if (_id != null) {
			WorkDeliverableDefinition deliverableDef = ModelService
					.createModelObject(WorkDeliverableDefinition.class, _id);
			WorkDocumentDefinition documentDef = deliverableDef
					.getWorkDocumentDefinition();
			if (documentDef != null) {
				po = documentDef.makeDocument(po);
				po.setValue(Document.F_DOCUMENT_NUMBER,
						getValue(F_DOCUMENT_NUMBER));
				po.setValue(Document.F_FOLDER_ID, getValue(F_FOLDER_ID));
				po.setValue(Document.F_DESC, getValue(F_DESC));
				po.setValue(Document.F__SUMMARY, getValue(F__SUMMARY));
				return po;
			}
		}
		return null;
	}
}
